package controller;

import helper.UserSession;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import model.Model;
import databeans.Customer;
import databeans.Employee;


public class EmployeeLogoutAction extends Action {
    public EmployeeLogoutAction(Model model) {

        model.getEmployeeDAO();
    }

    public String getName() {
        return "employeeLogout.do";
    }

    public String perform(HttpServletRequest request) {
        //String loginPage = "employee/login-form.jsp";
        //String dashboardPage = "employee/employee-dashboard.jsp";
    	String loginPage = "employee/employee-login.jsp";
    	String dashboardPage = "employee/employee-dashboard.jsp";
        String originPage = dashboardPage;
        String successPage = loginPage; 
        
        //set the employee
        Employee employee = null;
        Customer customer = null;

        
        //errors and messages will be stored here
        //resetting those containers
        List<String> errors = new ArrayList<String>();
        List<String> messages = new ArrayList<String>();
   
        //define the error and message
        request.setAttribute("errors", errors);
        request.setAttribute("messages", messages);
        request.setAttribute("page", "logout");
        
        //check if employee has been logged in
        //request.getSession() will return the current session and if one does not exist, a new session will be cretaed.
        //request.getSession(true) will return the current session if one exists, if one doesn't exits a new one will be created.
        //So there is actually no difference between the two methods HOWEVER, if you use request.getSession(false), it will return the current session if one exists and if one DOES NOT exist a new one will NOT be cretaed.
        employee = UserSession.getLoggedInEmployee(request);
        if (employee == null) {
            messages.add("You need to log in before able to log out");
            errors.add("No employee is currently logged in");
            return loginPage ;
        }
        
        //check if customer has actually logged in
        customer = UserSession.getLoggedInCustomer(request);
        if (customer != null) {
            request = UserSession.removeCustomerFromSession(request);
            errors.add("This section is closed for customer");
            return loginPage;
        }


        // at this stage, all validation should have been done
        try {
    
            
            // get the current session and strip it
            HttpSession session = request.getSession(true); 
            session.removeAttribute("employee");
            
            //set the welcome message
            //messages.add("Hi " + employee.getFirstName() + ", you have succesfully logged out from Carnegie Financial Service's iFund");
            messages.add("You have been succesfully logged out from Carnegie Financial Service's iFund");
            return successPage;

        } catch (Exception e) {
            errors.add("Unable to log out");
            return originPage;
        }

    }
}